﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Facebook;

namespace WebApp.Models
{
    public class Search<T> where T : ISearchable
    {
        private Type type;
        private IFactory<T> factory;

        public Search(Type type, IFactory<T> factory)
        {
            this.type = type;
            this.factory = factory;
        }

        public enum Type
        {
            Post,
            User,
            Page,
            Event,
            Group,
            Checkin
        };

        public List<T> PerformSearch(String query, String authToken)
        {
            FacebookClient fbc = new FacebookClient(authToken);
            String typeString = Enum.GetName(typeof(Type), type);
            dynamic items = fbc.Get("search?q=" + query + "&type=" + typeString);
            items = items["data"];
            List<T> result = new List<T>();
            foreach (dynamic item in items)
            {
                T itemObj = factory.Create(item["id"]);
                itemObj.Populate(authToken);
                result.Add(itemObj);
            }
            return result;
        }
    }
}